#!/usr/bin/python
# -*- coding: UTF-8 -*-
import pymysql
# 打开数据库连接
db = pymysql.connect(host="172.31.11.63", port=3306, user="djangotest", password="P52koMfruQGpWG226XqxLf3J", db="djangotest")

# 使用cursor()方法获取操作游标
cursor = db.cursor()

# sql 查询语句
sql = """select * from EMPLOYEE where INCOME > %s""" %(1000)

try:
    # 执行sql语句
    cursor.execute(sql)
    # 获取所有记录(结果是保存在元组里面的)
    results = cursor.fetchall()
    # 通过for循环取出每个字段
    for row in results:
        id = row[0]
        fname = row[1]
        lname = row[2]
        age = row[3]
        sex = row[4]
        income = row[5]
        # 打印结果
        print("id=%s, fname=%s, lname=%s, age=%s, sex=%s, income=%s" %(id, fname, lname, age, sex, income))
except:
    print("Error: unable to fetch data")

# 关闭数据库连接
db.close()